#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstring>
#include <experimental/filesystem>
#include <memory>
#include <sstream>

namespace cloud
{
    namespace fs = std::experimental::filesystem;
    class FileUtil
    {
    public:
        FileUtil(const std::string& filename)
            :_filename(filename)
        {}
        int64_t FileSize()
        {
            struct stat st;
            if (stat(_filename.c_str(), &st) < 0)
            {
                std::cout << "stat error" << std::endl;
                return -1;
            }
            return st.st_size;
        }
        time_t LastMTime()
        {
            struct stat st;
            if (stat(_filename.c_str(), &st) < 0)
            {
                std::cout << "stat error" << std::endl;
                return 0;
            }
            
            return st.st_mtime;
        }
        time_t LastATime()
        {
            struct stat st;
            if (stat(_filename.c_str(), &st) < 0)
            {
                std::cout << "stat error" << std::endl;
                return 0;
            }
            return st.st_atime;
        }
        std::string FileName()
        {
            size_t pos = _filename.rfind('\\');
            if (pos == std::string::npos)
            {
                return _filename;
            }
            std::string filename = _filename.substr(pos + 1);
            //return fs::path(_filename).string();
            return filename;
        }
        bool SetContent(const std::string& body)
        {
            std::ofstream ofs;
            ofs.open(_filename, std::ios::binary);
            if (ofs.is_open() == false)
            {
                std::cout << "open error" << std::endl;
                return false;
            }
            ofs.write(&body[0], body.size());
            if (ofs.good() == false)
            {
                std::cout << "write error" << std::endl;
                ofs.close();
                return false;
            }
            ofs.close();
            return true;
        }
        bool GetContent(std::string* body)
        {
            size_t fsize = this->FileSize();
            return GetPosLen(body, 0, fsize);
        }
        bool GetPosLen(std::string* body, size_t pos, size_t len)
        {
            std::ifstream ifs;
            ifs.open(_filename, std::ios::binary);
            if (ifs.is_open() == false)
            {
                std::cout << "open error" << std::endl;
                return false;
            }
            size_t fsize = this->FileSize();
            if (pos + len > fsize)
            {
                std::cout << "get file len is error\n";
                return false;
            }
            ifs.seekg(pos, std::ios::beg);
            body->resize(len);
            ifs.read(&(*body)[0], len);
            if (ifs.good() == false)
            {
                std::cout << "read error\n";
                ifs.close();
                return false;
            }
            ifs.close();
            return true;
        }
        bool Exists()
        {
            return fs::exists(_filename);
        }
        bool CreateDirectory()
        {
            if (this->Exists() == true) return true;
            return fs::create_directories(_filename);
        }
        bool GetDirectory(std::vector<std::string>* arry)
        {
            CreateDirectory();
            for (auto& e : fs::directory_iterator(_filename))
            {
                if (fs::is_directory(e) == true) continue;
                arry->push_back(fs::path(e).relative_path().string());
            }
            return true;
        }
        bool Remove()
        {
            if (Exists() == false)
            {
                return true;
            }
            remove(_filename.c_str());
            return true;
        }
        ~FileUtil()
        {}
    private:
        std::string _filename;
    };
}